package com.shuwen.cdp.track.controller;

import com.alibaba.fastjson.JSON;
import com.shuwen.cdp.track.entity.DataEntity;
import com.shuwen.cdp.track.service.UserLogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Base64;

@RestController
@RequestMapping(value = "/track")
public class TrackController {

    @Resource
    private UserLogService userLogService;

    @RequestMapping(value = "/sa.gif", method = RequestMethod.GET)
    @ResponseBody
    public void track(@RequestParam(value = "data",required = false) String data,
                          @RequestParam(value = "project",required = false) String project,
                          HttpServletRequest request) {
        try{
            String ip = request.getRemoteAddr();
            String logInfo = new String(Base64.getDecoder().decode(data));
            System.out.println(logInfo);
            //本地文件记录用户行为日志
            //转成json格式，用于kafka发送
            DataEntity dataEntity =  JSON.parseObject(logInfo, DataEntity.class);
            dataEntity.setProject(project);
            userLogService.logInsert(dataEntity);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}